home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryByReference / ImageComponentByReferenceTest.java.z / ImageComponentByReferenceTest.java
Encoding:
Java Source  |  2003-08-08  |  9.0 KB  |  324 lines

  1. /*
  2.  *    @(#)ImageComponentByReferenceTest.java 1.14 02/10/21 13:41:45
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.geometry.*;
  45. import com.sun.j3d.utils.universe.*;
  46. import javax.media.j3d.*;
  47. import javax.vecmath.*;
  48. import javax.swing.*;
  49. import javax.swing.event.*;
  50. import javax.swing.border.*;
  51. import com.sun.j3d.utils.behaviors.mouse.*;
  52. import com.sun.j3d.utils.image.TextureLoader;
  53. import com.sun.j3d.utils.geometry.Sphere;
  54. import com.sun.j3d.utils.geometry.Box;
  55. import com.sun.j3d.utils.geometry.ColorCube;
  56. import java.awt.image.*;
  57. import java.awt.color.ColorSpace;
  58. import com.sun.j3d.utils.geometry.*;
  59.  
  60. public class ImageComponentByReferenceTest extends JApplet implements ActionListener {
  61.  
  62.     Shape3D s1,s2;
  63.     TextureLoader t0, t1, t2;
  64.     int count = 0;
  65.  
  66.     Appearance app = new Appearance();
  67.     BranchGroup objRoot = new BranchGroup();
  68.     TransformGroup objTrans = new TransformGroup();
  69.     BufferedImage bImage1;
  70.     TiledImage checkBoard;
  71.     boolean yUp = false;
  72.     boolean byRef = true;
  73.     JComboBox rasterType, texType;
  74.     ImageComponent2D[] image = new ImageComponent2D[8];
  75.     Appearance dummyApp = new Appearance();
  76.     Texture2D texOne, texCheckBoard;
  77.     javax.media.j3d.Raster raster;
  78.     Box textureCube;
  79.     Shape3D boxShape;
  80.     int w1 = 64, h1 = 32, checkw = 16 , checkh = 16;
  81.  
  82.     private java.net.URL texImage = null;
  83.  
  84.     private SimpleUniverse u = null;
  85.     
  86.     public BranchGroup createSceneGraph() {
  87.         objRoot = new BranchGroup();
  88.  
  89.         objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  90.         objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);
  91.  
  92.         objRoot.addChild(objTrans);
  93.  
  94.         BoundingSphere bounds =
  95.             new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  96.  
  97.     app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
  98.  
  99.  
  100.         textureCube = new Box(0.4f, 0.4f, 0.4f,
  101.                                   Box.GENERATE_TEXTURE_COORDS|
  102.                   Box.GENERATE_NORMALS, app);
  103.     boxShape = textureCube.getShape(Box.FRONT);
  104.     boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  105.     objTrans.addChild(textureCube);
  106.  
  107.     checkBoard = new TiledImage();
  108.     TextureLoader texLoader = new TextureLoader( texImage, this);
  109.     ImageComponent2D oneImage = texLoader.getImage();
  110.     bImage1 = oneImage.getImage();
  111.  
  112.     int index = 0;
  113.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  114.                       (RenderedImage)bImage1,
  115.                       false,
  116.                       true);
  117.  
  118.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  119.                       (RenderedImage)bImage1,
  120.                       true,
  121.                       true);
  122.  
  123.  
  124.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  125.                       (RenderedImage)bImage1,
  126.                       false,
  127.                       false);
  128.     
  129.     
  130.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  131.                       (RenderedImage)bImage1,
  132.                       true,
  133.                       false);
  134.  
  135.     createRaster(objRoot);
  136.  
  137.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  138.                       checkBoard,
  139.                       false,
  140.                       true);
  141.  
  142.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  143.                       checkBoard,
  144.                       true,
  145.                       true);
  146.  
  147.  
  148.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  149.                       checkBoard,
  150.                       false,
  151.                       false);
  152.     
  153.     
  154.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  155.                       checkBoard,
  156.                       true,
  157.                       false);
  158.  
  159.  
  160.  
  161.     texOne = new Texture2D(Texture.BASE_LEVEL,
  162.                       Texture.RGBA,
  163.                       image[2].getWidth(), image[2].getHeight());
  164.  
  165.     texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
  166.     texOne.setImage(0, image[2]);
  167.     
  168.         app.setTexture(texOne);
  169.  
  170.     texCheckBoard = new Texture2D(Texture.BASE_LEVEL,
  171.                       Texture.RGBA,
  172.                       image[4].getWidth(), image[4].getHeight());
  173.     
  174.     texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
  175.         objRoot.compile();
  176.     return objRoot;
  177.     }
  178.  
  179.     public void actionPerformed(ActionEvent e ) {
  180.     Object target = e.getSource();
  181.  
  182.     if (target == rasterType) {
  183.         if (rasterType.getSelectedIndex() < 4) {
  184.         raster.setSize(w1, h1);
  185.         }
  186.         else {
  187.         raster.setSize(checkw, checkh);
  188.         }
  189.         raster.setImage(image[rasterType.getSelectedIndex()]);
  190.     }
  191.     else if (target == texType) {
  192.         boxShape.setAppearance(dummyApp);
  193.         if (texType.getSelectedIndex() < 4) {
  194.         texOne.setImage(0, image[texType.getSelectedIndex()]);
  195.         app.setTexture(texOne);
  196.         }
  197.         else {
  198.         texCheckBoard.setImage(0, image[texType.getSelectedIndex()]);
  199.         app.setTexture(texCheckBoard);
  200.         }
  201.         
  202.         boxShape.setAppearance(app);
  203.     }
  204.  
  205.  
  206.     }
  207.  
  208.     JPanel createImagePanel() {
  209.     JPanel panel = new JPanel();
  210.     String texVals[] = { "One_Yup_ByCopy",
  211.                  "One_Yup_ByReference",
  212.                  "One_Ydown_ByCopy",
  213.                  "One_Ydown_ByReference",
  214.                  "Checkered_Yup_ByCopy",
  215.                  "Checkered_Yup_ByReference",
  216.                  "Checkered_Ydown_ByCopy",
  217.                  "Checkered_Ydown_ByReference"};
  218.     
  219.     rasterType = new JComboBox(texVals);
  220.     rasterType.setLightWeightPopupEnabled(false);
  221.     rasterType.addActionListener(this);
  222.     rasterType.setSelectedIndex(2);
  223.     panel.add(new JLabel("Raster Image"));     
  224.     panel.add(rasterType);
  225.  
  226.     texType = new JComboBox(texVals);
  227.     texType.setLightWeightPopupEnabled(false);
  228.     texType.addActionListener(this);
  229.     texType.setSelectedIndex(2);
  230.     panel.add(new JLabel("Texture Image"));     
  231.     panel.add(texType);
  232.     return panel;
  233.  
  234.     }
  235.  
  236.  
  237.  
  238.     public ImageComponentByReferenceTest() 
  239.     {
  240.     }
  241.  
  242.     public ImageComponentByReferenceTest(java.net.URL url) {
  243.         texImage = url;
  244.     }
  245.  
  246.     public void init() {
  247.         if (texImage == null) {
  248.         // the path to the image for an applet
  249.         try {
  250.             texImage = new java.net.URL(getCodeBase().toString() +
  251.                         "../images/one.jpg");
  252.         }
  253.         catch (java.net.MalformedURLException ex) {
  254.             System.out.println(ex.getMessage());
  255.         System.exit(1);
  256.         }
  257.     }
  258.  
  259.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  260.     BranchGroup scene = createSceneGraph();u = new SimpleUniverse(c);
  261.         u.getViewingPlatform().setNominalViewingTransform();
  262.         u.addBranchGraph(scene);
  263.     Container contentPane = getContentPane();
  264.     JPanel p = new JPanel();
  265.     BoxLayout boxlayout = new BoxLayout(p, 
  266.                         BoxLayout.Y_AXIS);
  267.     p.setLayout(boxlayout);
  268.         contentPane.add("Center", c);
  269.   
  270.     contentPane.add("South", p);
  271.     
  272.     p.add(createImagePanel());
  273.  
  274.     }
  275.  
  276.     public void destroy() {
  277.     u.cleanup();
  278.     }
  279.  
  280.     public static void main(String[] args) {
  281.         java.net.URL url = null;
  282.         // the path to the image file for an application
  283.     try {
  284.         url = new java.net.URL("file:../images/one.jpg");
  285.     }
  286.     catch (java.net.MalformedURLException ex) {
  287.         System.out.println(ex.getMessage());
  288.         System.exit(1);
  289.     }
  290.  
  291.         new MainFrame(new ImageComponentByReferenceTest(url), 800, 700);
  292.     }
  293.  
  294.    void createRaster( BranchGroup scene) {
  295.  
  296.  
  297.  
  298.     // Create raster geometries and shapes
  299.     Vector3f trans = new Vector3f( );
  300.     Transform3D tr = new Transform3D( );
  301.     TransformGroup tg;
  302.  
  303.     // Left
  304.     raster = new javax.media.j3d.Raster( );
  305.     raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
  306.     raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
  307.     raster.setPosition( new Point3f( -0.9f, 0.75f, 0.0f ) );
  308.     raster.setType( javax.media.j3d.Raster.RASTER_COLOR );
  309.     raster.setOffset( 0, 0 );
  310.  
  311.     raster.setSize( image[2].getWidth(), image[2].getHeight() );
  312.     raster.setImage( image[2] );
  313.     Shape3D sh = new Shape3D( raster, new Appearance( ) );
  314.     scene.addChild( sh );
  315.  
  316.  
  317.  
  318.     }
  319. }
  320.  
  321.  
  322.  
  323.  
  324.